package com.lmx.persistence.service;

import io.katharsis.queryParams.QueryParams;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.lmx.common.util.CommonUtil;
import com.lmx.persistence.service.common.BaseCrudServiceImpl;
import com.lmx.repoDomain.tmodel.FoodPlanDayRecord;
import com.lmx.repoDomain.tmodel.FoodPlanRecord;
import com.lmx.repoDomain.tmodel.unpersis.QueryParam;

/**
 * 老人用餐统计service实现
 *
 * @author ytche
 * @date 2017年8月30日
 */
@Service
public class FoodPlanRecordServiceImpl extends
		BaseCrudServiceImpl<FoodPlanRecord> implements FoodPlanRecordService {
	@Autowired
	private CustomerFoodPlanService planService;
	@Autowired
	private FoodService foodService;
	@Autowired
	private FoodPlanDayRecordService dayRecordService;

	@Override
	public Iterable<FoodPlanRecord> findDayRecord(QueryParams requestParams,
			Long tenantId) {
		QueryParam pageParam = new QueryParam(requestParams);
		List<FoodPlanRecord> records = Lists.newArrayList();
		List<FoodPlanDayRecord> dayRecords = this.dayRecordService
				.findDayRecord(tenantId, pageParam);
		this.transDayRecord2Record(dayRecords, records);
		return records;
	}

	@Override
	public Iterable<FoodPlanRecord> findWeakRecord(QueryParams requestParams,
			Long tenantId) {
		QueryParam queryParam = new QueryParam(requestParams);
		StringBuilder hql = new StringBuilder("from FoodPlanRecord record ");
		StringBuilder countHql = new StringBuilder(
				"select count(record) from FoodPlanRecord record ");
		Map<String, Object> valueMap = Maps.newHashMap();
		Map<String, String> filter = CommonUtil
				.getKeyMapByFilter(requestParams);

		Long queryDate = new Long(filter.get("diningDate")) * 1000;
		Integer weekBegin = CommonUtil.getWeekBegin(new Date(queryDate), true);
		Integer weekEnd = weekBegin + 7 * 24 * 3600;
		String conditionSql = " where diningDate >= :weekBegin and diningDate < :weekEnd ";
		String sortHql = " order by diningDate ";

		hql.append(conditionSql).append(sortHql);
		countHql.append(conditionSql).append(sortHql);
		valueMap.put("weekBegin", weekBegin);
		valueMap.put("weekEnd", weekEnd);
		return super.pageByHql(hql.toString(), countHql.toString(), queryParam,
				valueMap).getContent();
	}

	@Override
	public void recordFoodPlanOfWeek(Date date) {
		Integer weekBegin = CommonUtil.getWeekBegin(date, true);
		Integer weekEnd = weekBegin + 7 * 3600 * 24;
		String sqlString = "INSERT INTO tnb_food_plan_record (num, food_id,"
				+ " dining_date, del_status, tenant_id) SELECT COUNT(*) AS num,"
				+ " rel.food_id,:weekBegin, 0, plan.tenant_id FROM tnb_customer_food_plan plan,"
				+ " rel_food_plan rel, tnb_food food WHERE plan.id = rel.plan_id"
				+ " AND food.id = food_id AND plan.dining_date >= :weekBegin"
				+ " AND plan.dining_date<:weekEnd GROUP BY food_id";
		super.em.createNativeQuery(sqlString)
				.setParameter("weekBegin", weekBegin)
				.setParameter("weekEnd", weekEnd).executeUpdate();
	}

	/**
	 * 将日统计信息翻译为统计信息
	 *
	 * @author ytche
	 * @param dayRecords
	 * @param records
	 * @date 2017年9月4日 上午10:57:10
	 */
	private void transDayRecord2Record(List<FoodPlanDayRecord> dayRecords,
			List<FoodPlanRecord> records) {
		for (FoodPlanDayRecord dayRecord : dayRecords) {
			FoodPlanRecord record = new FoodPlanRecord();
			BeanUtils.copyProperties(dayRecord, record);
			records.add(record);
		}
	}
}
